unit LoginForm;

interface

uses 
  SmartCL.System, SmartCL.Graphics, SmartCL.Components, SmartCL.Forms,
  SmartCL.Fonts, SmartCL.Borders, SmartCL.Application,
  SynCrossPlatformREST, SmartTests,
  SmartCL.Controls.Label, SmartCL.Controls.Editbox, SmartCL.Controls.Panel,
  SmartCL.Controls.Button, w3c.date;

type
  TLoginForm=class(TW3form)
    procedure W3Button1Click(Sender: TObject);
    procedure ConnectClick(Sender: TObject);
  private
    {$I 'LoginForm:intf'}
  protected
    procedure InitializeForm; override;
    procedure InitializeObject; override;
    procedure Resize; override;
  end;

implementation

uses
  mORMotClient; // unit generated by the server!

{ TLoginForm }

procedure TLoginForm.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
end;

procedure TLoginForm.InitializeObject;
begin
  inherited;
  {$I 'LoginForm:impl'}
  LogonPassword.InputType := itPassword;
end;

procedure TLoginForm.ConnectClick(Sender: TObject);
var model: TSQLModel;
begin
  BrowserAPI.console.time('ORM');
  writeln('Creating Data Model');
  model := GetModel;
  model.GetTableIndexExisting(TSQLRecordPeople);
  var people := new TSQLRecordPeople;
  var s := model.InfoExisting(people.RecordClass).ToJSONAdd(nil,people,true,'');
  assert(s='{"RowID":0,"FirstName":"","LastName":"","YearOfBirth":0,"YearOfDeath":0,'+
    '"Sexe":0,"Simple":{"F":"","G":[],"H":{"H1":0,"H2":"","H3":{"H3a":false,"H3b":null}},"I":"","J":[]}}');
  s := '{"RowID":10,"FirstName":"ab\"c","LastName":"def","YearOfBirth":20,"YearOfDeath":30,'+
    '"Sexe":1,"Simple":{"F":"","G":[],"H":{"H1":0,"H2":"","H3":{"H3a":false,"H3b":null}},"I":"","J":['+
      '{"J1":1,"J2":"","J3":"reLast"}]}}';
  assert(people.FromJSON(s));
  assert(people.ID=10);
  assert(people.FirstName='ab"c');
  assert(people.LastName='def');
  assert(people.YearOfBirth=20);
  assert(people.YearOfDeath=30);
  assert(people.Sexe=sMale);
  assert(people.Simple.J.Count=1);
  assert(people.Simple.J[0].J1=1);
  assert(people.Simple.J[0].J3=reLast);
  writeln('Connecting to the server at '+ServerAddress.Text+':888');
  GetClient(ServerAddress.Text,LogonName.Text,LogonPassword.Text,
  lambda(client)
    client.LogToRemoteServer(LOG_VERBOSE,'localhost');
    writeln('Safely connected with SessionID='+IntToStr(client.Authentication.SessionID));
    people := TSQLRecordPeople.Create(client,1);
    assert(people.ID=1);
    writeln(people.ToJSON(client.Model,'*'));
    writeln('Testing remote CRUD methods');
    ORMTest(client);
    BrowserAPI.console.timeEnd('ORM');
    BrowserAPI.console.time('SOA');
    writeln('Testing SOA remote access');
    SOATest(client,
    lambda
      writeln('Disconnect from server');
      client.Free;
      BrowserAPI.console.timeEnd('SOA');
    end,
    lambda
      writeln('ERROR!');
      writeln('Disconnect from server');
      client.Free;
      BrowserAPI.console.timeEnd('SOA');
    end);
  end,
  lambda
    ShowMessage('Impossible to connect to the server');
    writeln('ERROR at GetClient');
    BrowserAPI.console.timeEnd('ORM');
  end);
end;

procedure TLoginForm.W3Button1Click(Sender: TObject);
begin
  BrowserAPI.console.time('LowLevel');
  TestSMS;
  BrowserAPI.console.timeEnd('LowLevel');
end;

procedure TLoginForm.Resize;
begin
  inherited;
end;
 
end.
